package com.douziit.meilianoa.base.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by showlin on 2019-03-08.
 */
public class MyPubUtil {

    private static final Logger logger = LoggerFactory.getLogger(MyPubUtil.class);

    private static char[] crcDictionary = {0xF078, 0xE1F1, 0xD36A, 0xC2E3, 0xB65C, 0xA7D5,
            0x954E, 0x84C7, 0x7C30, 0x6DB9, 0x5F22, 0x4EAB, 0x3A14, 0x2B9D,
            0x1906, 0x088F, 0xE0F9, 0xF170, 0xC3EB, 0xD262, 0xA6DD, 0xB754,
            0x85CF, 0x9446, 0x6CB1, 0x7D38, 0x4FA3, 0x5E2A, 0x2A95, 0x3B1C,
            0x0987, 0x180E, 0xD17A, 0xC0F3, 0xF268, 0xE3E1, 0x975E, 0x86D7,
            0xB44C, 0xA5C5, 0x5D32, 0x4CBB, 0x7E20, 0x6FA9, 0x1B16, 0x0A9F,
            0x3804, 0x298D, 0xC1FB, 0xD072, 0xE2E9, 0xF360, 0x87DF, 0x9656,
            0xA4CD, 0xB544, 0x4DB3, 0x5C3A, 0x6EA1, 0x7F28, 0x0B97, 0x1A1E,
            0x2885, 0x390C, 0xB27C, 0xA3F5, 0x916E, 0x80E7, 0xF458, 0xE5D1,
            0xD74A, 0xC6C3, 0x3E34, 0x2FBD, 0x1D26, 0x0CAF, 0x7810, 0x6999,
            0x5B02, 0x4A8B, 0xA2FD, 0xB374, 0x81EF, 0x9066, 0xE4D9, 0xF550,
            0xC7CB, 0xD642, 0x2EB5, 0x3F3C, 0x0DA7, 0x1C2E, 0x6891, 0x7918,
            0x4B83, 0x5A0A, 0x937E, 0x82F7, 0xB06C, 0xA1E5, 0xD55A, 0xC4D3,
            0xF648, 0xE7C1, 0x1F36, 0x0EBF, 0x3C24, 0x2DAD, 0x5912, 0x489B,
            0x7A00, 0x6B89, 0x83FF, 0x9276, 0xA0ED, 0xB164, 0xC5DB, 0xD452,
            0xE6C9, 0xF740, 0x0FB7, 0x1E3E, 0x2CA5, 0x3D2C, 0x4993, 0x581A,
            0x6A81, 0x7B08, 0x7470, 0x65F9, 0x5762, 0x46EB, 0x3254, 0x23DD,
            0x1146, 0x00CF, 0xF838, 0xE9B1, 0xDB2A, 0xCAA3, 0xBE1C, 0xAF95,
            0x9D0E, 0x8C87, 0x64F1, 0x7578, 0x47E3, 0x566A, 0x22D5, 0x335C,
            0x01C7, 0x104E, 0xE8B9, 0xF930, 0xCBAB, 0xDA22, 0xAE9D, 0xBF14,
            0x8D8F, 0x9C06, 0x5572, 0x44FB, 0x7660, 0x67E9, 0x1356, 0x02DF,
            0x3044, 0x21CD, 0xD93A, 0xC8B3, 0xFA28, 0xEBA1, 0x9F1E, 0x8E97,
            0xBC0C, 0xAD85, 0x45F3, 0x547A, 0x66E1, 0x7768, 0x03D7, 0x125E,
            0x20C5, 0x314C, 0xC9BB, 0xD832, 0xEAA9, 0xFB20, 0x8F9F, 0x9E16,
            0xAC8D, 0xBD04, 0x3674, 0x27FD, 0x1566, 0x04EF, 0x7050, 0x61D9,
            0x5342, 0x42CB, 0xBA3C, 0xABB5, 0x992E, 0x88A7, 0xFC18, 0xED91,
            0xDF0A, 0xCE83, 0x26F5, 0x377C, 0x05E7, 0x146E, 0x60D1, 0x7158,
            0x43C3, 0x524A, 0xAABD, 0xBB34, 0x89AF, 0x9826, 0xEC99, 0xFD10,
            0xCF8B, 0xDE02, 0x1776, 0x06FF, 0x3464, 0x25ED, 0x5152, 0x40DB,
            0x7240, 0x63C9, 0x9B3E, 0x8AB7, 0xB82C, 0xA9A5, 0xDD1A, 0xCC93,
            0xFE08, 0xEF81, 0x07F7, 0x167E, 0x24E5, 0x356C, 0x41D3, 0x505A,
            0x62C1, 0x7348, 0x8BBF, 0x9A36, 0xA8AD, 0xB924, 0xCD9B, 0xDC12,
            0xEE89, 0xFF00};

    public static byte[] getCRCl6(byte[] bytes) {
        int crc = 0x0000;

        for (byte b :
                bytes) {
            crc = (crc >>> 8) ^ crcDictionary[(crc ^ b) & 0xff];
        }

        byte[] rr = intToBytes(crc);

        return rr;
    }

    /**
     * hexString转byte数组
     *
     * @param hexString
     * @return
     */
    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToBytes(hexChars[pos]) << 4 | charToBytes(hexChars[pos + 1]));

        }
        return d;
    }

    private static byte charToBytes(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }

    public static byte[] str2Bcd(String asc) {
        //获取字符串的长度
        int len = asc.length();
        //字符串长度取余2
        int mod = len % 2;
        //如果有余数，则高位加0

        if (mod != 0) {
            asc = "0" + asc;
            len = asc.length();
        }

        byte[] abt = new byte[len];

        if (len >= 2) {
            len = len / 2;
        }

        byte[] bbt = new byte[len];

        abt = asc.getBytes();

        int j, k;

        for (int p = 0; p < asc.length() / 2; p++) {
            if ((abt[2 * p] >= '0') && (abt[2 * p] <= '9')) {
                j = abt[2 * p] - '0';
            } else if ((abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) {
                j = abt[2 * p] - 'a' + 0x0a;
            } else {
                j = abt[2 * p] - 'A' + 0x0a;
            }

            if ((abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) {
                k = abt[2 * p + 1] - '0';
            } else if ((abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) {
                k = abt[2 * p + 1] - 'a' + 0x0a;
            } else {
                k = abt[2 * p + 1] - 'A' + 0x0a;
            }

            int a = (j << 4) + k;
            byte b = (byte) a;
            bbt[p] = b;
        }
        return bbt;
    }

    /**
     * (ASCII码)字符数组转字符串
     *
     * @param bytes
     * @return
     */
    public static String ascIIToString(byte[] bytes) {
        String string;

        StringBuffer stringBuffer = new StringBuffer();

        char[] chars = new char[bytes.length];

        for (int i = 0; i < bytes.length; i++) {
            chars[i] = (char) bytes[i];

        }

        stringBuffer.append(chars);
        string = stringBuffer.toString();

        return string;
    }

    /**
     * 字符串转字符数组
     *
     * @param string 要转的字符串
     * @return 字符数组
     */
    public static byte[] stringToAscII(String string) {
        byte[] bytes = new byte[0];

        try {
            bytes = string.getBytes("US-ASCII");
        } catch (UnsupportedEncodingException e) {
            logger.error("string to ascii exception: str:{}", string, e);
        }
        return bytes;
    }


    /**
     * 字节数根据ASCII 码 转 BCD码
     *
     * @param asc 要转的字节数
     * @return 字节数
     */
    private static byte ascToBcd(byte asc) {
        byte bcd;

        if ((asc >= '0') && (asc <= '9')) {
            bcd = (byte) (asc - '0');
        } else if ((asc >= 'A') && (asc <= 'F')) {
            bcd = (byte) (asc - 'A' + 10);
        } else if ((asc >= 'a') && (asc <= 'f')) {
            bcd = (byte) (asc - 'a' + 10);
        } else {
            bcd = (byte) (asc - 48);
        }
        return bcd;
    }

    public static byte[] ascIIToBCD(byte[] ascii, int asc_len) {
        byte[] bcd = new byte[asc_len / 2];
        int j = 0;

        for (int i = 0; i < (asc_len + 1) / 2; i++) {
            bcd[i] = ascToBcd(ascii[j++]);
            bcd[i] = (byte) (((j >= asc_len) ? 0x00 : ascToBcd(ascii[i++])) + (bcd[i] << 4));
        }

        return bcd;
    }

    public static String bcd2Str(byte[] bytes) {
        char temp[] = new char[bytes.length * 2], val;

        for (int i = 0; i < bytes.length; i++) {
            val = (char) (((bytes[i] & 0xf0) >> 4) & 0x0f);
            temp[i * 2] = (char) (val > 9 ? val + 'A' - 10 : val + '0');

            val = (char) (bytes[i] & 0x0f);
            temp[i * 2 + 1] = (char) (val > 9 ? val + 'A' - 10 : val + '0');
        }

        return new String(temp);
    }

    /**
     * 将 int 数值转换为占四个字节的byte数组，本方法适用于(低位在前，高位在后)的顺序。和 bytesToInt() 配套使用
     *
     * @param value 要转换的int值
     * @return byte数组
     */
    public static byte[] intToBytes(int value) {
        byte[] bytes = new byte[4];
        bytes[3] = (byte) ((value >> 24) & 0xFF);
        bytes[2] = (byte) ((value >> 16) & 0xFF);
        bytes[1] = (byte) ((value >> 8) & 0xFF);
        bytes[0] = (byte) (value & 0xFF);

        return bytes;
    }

    /**
     * 将int数值转换为占四个字节的byte数据，本方法适用于(高位在后，低位在前)的顺序。和bytesToInt2 () 配套使用
     *
     * @param value 要转的int 值
     * @return byte 数组
     */
    public static byte[] intToBytes2(int value) {
        byte[] bytes = new byte[4];
        bytes[0] = (byte) (value & 0xFF);
        bytes[1] = (byte) ((value >> 8) & 0xFF);
        bytes[2] = (byte) ((value >> 16) & 0xFF);
        bytes[3] = (byte) ((value >> 24) & 0xFF);

        return bytes;
    }


    public static byte[] intToBytes8(int value) {
        byte[] bytes = new byte[8];
        bytes[0] = (byte) (value & 0xFF);
        bytes[1] = (byte) ((value >> 8) & 0xFF);
        bytes[2] = (byte) ((value >> 16) & 0xFF);
        bytes[3] = (byte) ((value >> 24) & 0xFF);
        bytes[4] = (byte) ((value >> 32) & 0xFF);
        bytes[5] = (byte) ((value >> 40) & 0xFF);
        bytes[6] = (byte) ((value >> 48) & 0xFF);
        bytes[7] = (byte) ((value >> 56) & 0xFF);

        return bytes;
    }

    /**
     * 将int数值转换为占两个字节的byte数据，本方法适用于(高位在后，低位在前)的顺序。和bytesToInt2 () 配套使用
     *
     * @param value 要转的int 值
     * @return byte 数组
     */
    public static byte[] intToBytes3(int value) {
        byte[] bytes = new byte[2];
        bytes[0] = (byte) (value & 0xFF);
        bytes[1] = (byte) ((value >> 8) & 0xFF);
        return bytes;
    }

    /**
     * 将int数值转换为占一个字节的byte数据
     *
     * @param value 要转的int 值
     * @return byte 数组
     */
    public static byte[] intToBytes1(int value) {
        byte[] bytes = new byte[1];
        bytes[0] = (byte) (value & 0xFF);
        return bytes;
    }



    /**
     * byte 数组中取 int 数值，本方法适用于(低位在前，高位在后)的顺序,和 intToBytes() 配套使用
     *
     * @param bytes
     * @return
     */
    public static int bytesToInt(byte[] bytes) {
        int value;
        value = (int) ((bytes[0] & 0xFF));
        return value;
    }

    /**
     * byte数组中取int数值，本方法适用于(低位在前，高位在后)的顺序。和intToBytes2（）配套使用
     */
    public static int bytesToInt2(byte[] src) {
        int value;
        value = (int) ((src[0] & 0xFF)
                | ((src[1] & 0xFF) << 8)
                | ((src[2] & 0xFF) << 16)
                | ((src[3] & 0xFF) << 24));
        return value;
    }

    /**
     * byte数组中取int数值，本方法适用于(低位在后，高位在前)的顺序。和intToBytes2（）配套使用
     */
    public static int oneByteToInt2(byte b) {
        int value;
        byte[] src = new byte[]{0x00, 0x00, 0x00, b};
        value = (int) (((src[0] & 0xFF) << 24)
                | ((src[1] & 0xFF) << 16)
                | ((src[2] & 0xFF) << 8)
                | (src[3] & 0xFF));
        return value;
    }

    public static int twoByteToInt2(byte[] b) {
        int value;
        byte[] src = new byte[]{0x00, 0x00, b[0], b[1]};
        value = (int) (((src[0] & 0xFF) << 24)
                | ((src[1] & 0xFF) << 16)
                | ((src[2] & 0xFF) << 8)
                | (src[3] & 0xFF));
        return value;
    }

    public static long toLong(byte[] b) {
        long l = 0;
        l = b[0];
        l |= ((long) b[1] << 8);
        l |= ((long) b[2] << 16);
        l |= ((long) b[3] << 24);
        l |= ((long) b[4] << 32);
        l |= ((long) b[5] << 40);
        l |= ((long) b[6] << 48);
        l |= ((long) b[7] << 56);
        return l;
    }

    public byte[] toByteArray(int number) {
        int temp = number;
        byte[] b = new byte[8];
        for (int i = b.length - 1; i > -1; i--) {
            b[i] = new Integer(temp & 0xff).byteValue();
            temp = temp >> 8;
        }
        return b;
    }

    public static String bytesToHexString(byte[] src) {
        if (src == null || src.length <= 0) {
            return null;
        }

        StringBuilder stringBuilder = new StringBuilder("");
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

    /**
     * char 转 byte数组
     *
     * @param c
     * @return
     */
    public static byte[] charToByte(char c) {
        byte[] bytes = new byte[2];
        bytes[0] = (byte) ((c & 0xFF00) >> 8);
        bytes[1] = (byte) (c & 0xFF);

        return bytes;
    }

    /**
     * byte转char
     *
     * @param
     * @return
     */
    public static char byteToChar(byte[] b) {
        char c = (char) (((b[0] & 0xFF) << 8) | (b[1] & 0xFF));
        return c;
    }

    /**
     * 低位优先 转 高位优先
     *
     * @param
     * @return
     * @throws UnsupportedEncodingException
     */
    public static byte[] littleEndianToBigEndian(byte[] bs) {
        return new StringBuffer(new String(bs)).reverse().toString().getBytes();
    }

    /**
     * 获得字符串的字节长度，中文字符算做两个字节
     *
     * @param s
     * @return
     * @throws UnsupportedEncodingException
     */
    public static int getWordCount(String s) throws UnsupportedEncodingException {
        return s.getBytes("GBK").length;
    }

    public static String getCurrentTime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
        return df.format(new Date());
    }

    public static String getCurrentSQLTime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(new Date());
    }

    public static String getCurrentTimeWithoutDate() {
        SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");//设置日期格式
        return df.format(new Date());
    }

    public static String getCurrentDate() {
        String temp_str = "";
        Date dt = new Date();
        //最后的aa表示“上午”或“下午”    HH表示24小时制    如果换成hh表示12小时制
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        temp_str = sdf.format(dt);
        return temp_str;
    }

    /**
     * 判断source是否大于当前时间
     *
     * @param source
     * @return
     */
    public static Boolean sourceBiggerThanCurrent(String source) {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        try {
            c1.setTime(df.parse(source));
        } catch (java.text.ParseException e) {
            System.err.println("日期格式不正确");
        }
        int result = c1.compareTo(c2);
        if (result == 0) {
            System.out.println("source相等now");
            return false;
        } else if (result < 0) {
            System.out.println("source小于now");
            return false;
        } else {
            System.out.println("source大于now");
            return true;
        }
    }

    /**
     * 判断source是否大于targe
     *
     * @param source
     * @param target
     * @return
     */
    public static Boolean sourceBiggerThanTarget(String source, String target) {
        // String s1 = "2008-01-25 09:12:09";
        //String s2 = "2008-01-29 09:12:11";

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        try {
            c1.setTime(df.parse(source));
            c2.setTime(df.parse(target));
        } catch (java.text.ParseException e) {
            System.err.println("日期格式不正确");
        }
        int result = c1.compareTo(c2);
        if (result == 0) {
            //System.out.println("source相等target");
            return false;
        } else if (result < 0) {
            //System.out.println("source小于target");
            return false;
        } else {
            //System.out.println("source大于target");
            return true;
        }
    }

    /**
     * byte 数组转成long
     *
     * @param b
     * @return
     */
    public static long byteToLong(byte[] b) {
        long s = 0;
        long s0 = b[0] & 0xff;// 最低位
        long s1 = b[1] & 0xff;
        long s2 = b[2] & 0xff;
        long s3 = b[3] & 0xff;
        long s4 = b[4] & 0xff;// 最低位
        long s5 = b[5] & 0xff;
        long s6 = b[6] & 0xff;
        long s7 = b[7] & 0xff;

        // s0不变
        s1 <<= 8;
        s2 <<= 16;
        s3 <<= 24;
        s4 <<= 8 * 4;
        s5 <<= 8 * 5;
        s6 <<= 8 * 6;
        s7 <<= 8 * 7;
        s = s0 | s1 | s2 | s3 | s4 | s5 | s6 | s7;
        return s;
    }

    private static String bytes2HexString(byte[] b) {
        String ret = "";
        for (int i = 0; i < b.length; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            ret += hex.toUpperCase();
        }
        return ret;
    }


    public static byte[] bytsArrayListTobyteArray(List<byte[]> bytebytes) {
        int size = 0;
        for (int i = 0; i < bytebytes.size(); i++) {
            size += bytebytes.get(i).length;
        }
        byte[] r = new byte[size];
        int pos = 0;
        for (int i = 0; i < bytebytes.size(); i++) {
            byte[] bytes = bytebytes.get(i);
            for (int m = 0; m < bytes.length; m++) {
                r[pos] = bytes[m];
                pos++;
            }
        }
        return r;
    }

    /**
     * 截取byte 数组
     *
     * @param src  要截取的byte数组
     * @param from 截取的开始位置
     * @param to   截止位置
     * @return byte 数组
     */
    public static byte[] subArray(byte[] src, int from, int to) {
        int length = to - from;
        if (length > 0) {
            byte[] bbb = new byte[length];
            System.arraycopy(src, from, bbb, 0, to - from);
            return bbb;
        } else {
            return null;
        }
    }

    /**
     * byte数组后面追加byte数组
     *
     * @param source 原始数组
     * @param target 要追加的数组
     * @return byte 数组
     */
    public static byte[] appendByteArray(byte[] source, byte[] target) {
        int l = source.length + target.length;
        byte[] result = new byte[l];
        System.arraycopy(source, 0, result, 0, source.length);
        System.arraycopy(target, 0, result, source.length, target.length);

        return result;
    }

    /**
     * byte数组插入byte数组
     *
     * @param source 原始数组
     * @param index  插入的下标位置
     * @param target 要插入的数组
     * @return byte 数组
     */
    public static byte[] insertByteArray(byte[] source, int index, byte[] target) {
        int l = source.length + target.length;
        byte[] result = new byte[l];
        System.arraycopy(source, 0, result, 0, index);
        System.arraycopy(target, 0, result, index, target.length);
        System.arraycopy(source, index, result, index + target.length, source.length - index);

        return result;
    }

    //byte数组删除一位

    /**
     * byte数组删除一位
     *
     * @param source 要删除一位的数组
     * @param index  一位的下标
     * @return byte 数组
     */
    public static byte[] deleteByteArray(byte[] source, int index) {
        int l = source.length - 1;
        byte[] result = new byte[l];
        System.arraycopy(source, 0, result, 0, index);
        System.arraycopy(source, index + 1, result, index, l - index);

        return result;
    }

    public static String getCardCode(int aaa) {
        String card_code = aaa + "";
        if (aaa < 0) {
            byte[] bbb = MyPubUtil.intToBytes(aaa);
            byte[] bbbb = new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
            for (int i = 0; i < 4; i++) {
                bbbb[i] = bbb[i];
            }

            long aa = MyPubUtil.byteToLong(bbbb);

            card_code = aa + "";
        }

        return card_code;
    }

    /**
     * 将int数值转换为占四个字节的byte数组，本方法适用于(高位在前，低位在后)的顺序。  和bytesToInt2（）配套使用
     */
    public static byte[] longToBytes2(long value) {
        byte[] src = new byte[8];
        src[0] = (byte) (value & 0xFF);
        src[1] = (byte) ((value >> 8) & 0xFF);
        src[2] = (byte) ((value >> 16) & 0xFF);
        src[3] = (byte) ((value >> 24) & 0xFF);
        src[4] = (byte) ((value >> 32) & 0xFF);
        src[5] = (byte) ((value >> 40) & 0xFF);
        src[6] = (byte) ((value >> 48) & 0xFF);
        src[7] = (byte) ((value >> 56) & 0xFF);

        return src;
    }


    static int CRCTABLE[] = {
            0xF078, 0xE1F1, 0xD36A, 0xC2E3, 0xB65C, 0xA7D5, 0x954E, 0x84C7,
            0x7C30, 0x6DB9, 0x5F22, 0x4EAB, 0x3A14, 0x2B9D, 0x1906, 0x088F,
            0xE0F9, 0xF170, 0xC3EB, 0xD262, 0xA6DD, 0xB754, 0x85CF, 0x9446,
            0x6CB1, 0x7D38, 0x4FA3, 0x5E2A, 0x2A95, 0x3B1C, 0x0987, 0x180E,
            0xD17A, 0xC0F3, 0xF268, 0xE3E1, 0x975E, 0x86D7, 0xB44C, 0xA5C5,
            0x5D32, 0x4CBB, 0x7E20, 0x6FA9, 0x1B16, 0x0A9F, 0x3804, 0x298D,
            0xC1FB, 0xD072, 0xE2E9, 0xF360, 0x87DF, 0x9656, 0xA4CD, 0xB544,
            0x4DB3, 0x5C3A, 0x6EA1, 0x7F28, 0x0B97, 0x1A1E, 0x2885, 0x390C,
            0xB27C, 0xA3F5, 0x916E, 0x80E7, 0xF458, 0xE5D1, 0xD74A, 0xC6C3,
            0x3E34, 0x2FBD, 0x1D26, 0x0CAF, 0x7810, 0x6999, 0x5B02, 0x4A8B,
            0xA2FD, 0xB374, 0x81EF, 0x9066, 0xE4D9, 0xF550, 0xC7CB, 0xD642,
            0x2EB5, 0x3F3C, 0x0DA7, 0x1C2E, 0x6891, 0x7918, 0x4B83, 0x5A0A,
            0x937E, 0x82F7, 0xB06C, 0xA1E5, 0xD55A, 0xC4D3, 0xF648, 0xE7C1,
            0x1F36, 0x0EBF, 0x3C24, 0x2DAD, 0x5912, 0x489B, 0x7A00, 0x6B89,
            0x83FF, 0x9276, 0xA0ED, 0xB164, 0xC5DB, 0xD452, 0xE6C9, 0xF740,
            0x0FB7, 0x1E3E, 0x2CA5, 0x3D2C, 0x4993, 0x581A, 0x6A81, 0x7B08,
            0x7470, 0x65F9, 0x5762, 0x46EB, 0x3254, 0x23DD, 0x1146, 0x00CF,
            0xF838, 0xE9B1, 0xDB2A, 0xCAA3, 0xBE1C, 0xAF95, 0x9D0E, 0x8C87,
            0x64F1, 0x7578, 0x47E3, 0x566A, 0x22D5, 0x335C, 0x01C7, 0x104E,
            0xE8B9, 0xF930, 0xCBAB, 0xDA22, 0xAE9D, 0xBF14, 0x8D8F, 0x9C06,
            0x5572, 0x44FB, 0x7660, 0x67E9, 0x1356, 0x02DF, 0x3044, 0x21CD,
            0xD93A, 0xC8B3, 0xFA28, 0xEBA1, 0x9F1E, 0x8E97, 0xBC0C, 0xAD85,
            0x45F3, 0x547A, 0x66E1, 0x7768, 0x03D7, 0x125E, 0x20C5, 0x314C,
            0xC9BB, 0xD832, 0xEAA9, 0xFB20, 0x8F9F, 0x9E16, 0xAC8D, 0xBD04,
            0x3674, 0x27FD, 0x1566, 0x04EF, 0x7050, 0x61D9, 0x5342, 0x42CB,
            0xBA3C, 0xABB5, 0x992E, 0x88A7, 0xFC18, 0xED91, 0xDF0A, 0xCE83,
            0x26F5, 0x377C, 0x05E7, 0x146E, 0x60D1, 0x7158, 0x43C3, 0x524A,
            0xAABD, 0xBB34, 0x89AF, 0x9826, 0xEC99, 0xFD10, 0xCF8B, 0xDE02,
            0x1776, 0x06FF, 0x3464, 0x25ED, 0x5152, 0x40DB, 0x7240, 0x63C9,
            0x9B3E, 0x8AB7, 0xB82C, 0xA9A5, 0xDD1A, 0xCC93, 0xFE08, 0xEF81,
            0x07F7, 0x167E, 0x24E5, 0x356C, 0x41D3, 0x505A, 0x62C1, 0x7348,
            0x8BBF, 0x9A36, 0xA8AD, 0xB924, 0xCD9B, 0xDC12, 0xEE89, 0xFF00
    };

    public static int twCRC16(byte[] data, int nBytes) {
        int CRCVal = 0;
        int i = 0;
        for (i = 0; i < nBytes; i++) {
            int mapVal = CRCTABLE[(CRCVal ^= ((data[i]) & 0xFF)) & 0xFF];
            CRCVal = mapVal ^ (CRCVal >> 8);
        }
        return CRCVal & 0xffff;
    }

    /**
     * 将16进制字符串转换为10进制数字
     * @param hexs
     * @return
     */
    public static int decodeHex(String hexs){
        BigInteger bigint=new BigInteger(hexs, 16);
        int numb=bigint.intValue();
        return numb;
    }

    /**
     * 将10进制转换为16进制
     * @param numb
     * @return
     */
    public static String encodeHex(Integer numb){
        String hex= Integer.toHexString(numb);
        return hex;
    }

    /**
     * 将16进制转换为2进制
     * @param hexs
     * @return
     */
    public static String encode2Hex(String hexs){
        int i = Integer.parseInt(hexs, 16);
        String str2 = Integer.toBinaryString(i);
        return str2;
    }

    /**
     * 将2进制转换成16进制
     *
     * @param buf
     * @return
     */
    public static String parseByte2HexStr(byte buf[]) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }


    /**
     * 二进制字符串转换为byte数组,每个字节以","隔开
     **/
    public static byte[] conver2HexToByte(String hex2Str) {
        String[] temp = hex2Str.split(",");
        byte[] b = new byte[temp.length];
        for (int i = 0; i < b.length; i++) {
            b[i] = Long.valueOf(temp[i], 2).byteValue();
        }
        return b;
    }



    public static void main(String[] args) throws UnsupportedEncodingException {
       // String ss = "1A CF 02 00 03 81 A6 00 00 5A 5A 5C 3D 3F 00 03 07 E3 02 5D 00 24 00 00 00 00 01 05 00 0A 00 EA 00 00 00 00 00 00 00 12 5A 5C 3D 3F 00 03 07 E3 02 5D 00 53 00 00 00 00 01 05 00 0A 00 EA 00 00 00 00 00 00 00 11 5A 5C 3D 3F 00 03 07 E3 02 5D 00 40 00 00 00 00 01 05 00 0A 00 EA 00 00 00 00 00 00 00 12";
      //  byte[] bytes = MyPubUtil.hexStringToBytes("2019120645");
       // String s = MyPubUtil.bytesToHexString(bytes);
        String str = "00100000";
        byte[] bytes = conver2HexToByte(str);
        String s = parseByte2HexStr(bytes);
        System.out.println(bytes);
        System.out.println(s);

    }
}
